//
//  ZMDropdownView.m
//  DaMei
//
//  Created by mac on 16/2/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ZMDropdownView.h"
#import "ZMCategoryModel.h"
#import "ZMDropdownLeftTableViewCell.h"
#import "ZMDrodownRightTableViewCell.h"
#import "ZMDistrictModel.h"

@interface ZMDropdownView()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftView;
@property (weak, nonatomic) IBOutlet UITableView *rightView;

/** 分类选中的模型*/
@property (nonatomic, strong)  ZMCategoryModel *selectCategoryModel;

/** 区域选中的模型*/
@property (nonatomic, strong)  ZMDistrictModel *selectDistrictModel;

@end

@implementation ZMDropdownView

+(instancetype)dropdownView {

    return [[[NSBundle mainBundle] loadNibNamed:@"ZMDropdownView" owner:nil options:nil] lastObject];
}
-(void)awakeFromNib {

    self.autoresizingMask = UIViewAutoresizingNone;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.categoryArray.count) {
        
        if (tableView == self.leftView) {
            return self.categoryArray.count;
        }else{
            //
            //        ZMCategoryModel *categoryModel = self.categoryArray[self.selectLeftVCIndex];
            //
            //
            //    return categoryModel.subcategories.count;
            return self.selectCategoryModel.subcategories.count;
        }
    } else {
    
        if (tableView == self.leftView) {
            return self.districtArray.count;
        } else {
        
            return self.selectDistrictModel.subdistricts.count;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    
    if (self.categoryArray.count) {
        
        if (tableView == self.leftView) {
            
            cell = [ZMDropdownLeftTableViewCell dropdownLeftTableViewCellWithTableView:tableView];
            
            ZMCategoryModel *categoryModel = self.categoryArray[indexPath.row];
            
            cell.textLabel.text = categoryModel.name;
            
            if (categoryModel.subcategories.count) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }else{
                
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            
            cell.imageView.image = [UIImage imageNamed:categoryModel.icon];
            cell.imageView.highlightedImage = [UIImage imageNamed:categoryModel.highlighted_icon];
            
        }else {
            
            cell = [ZMDrodownRightTableViewCell dropdownRightTableViewCellWithTableView:tableView];
            
//            ZMCategoryModel *categoryModel = self.categoryArray[self.selectLeftVCIndex];
//            
//            cell.textLabel.text = categoryModel.subcategories[indexPath.row];
            
            cell.textLabel.text = self.selectCategoryModel.subcategories[indexPath.row];
        }
    }else {
    
        if (tableView == self.leftView) {
            
            cell = [ZMDropdownLeftTableViewCell dropdownLeftTableViewCellWithTableView:tableView];
            
            //获取模型数据
            ZMDistrictModel *distrctModel = self.districtArray[indexPath.row];
            
            //给cell赋值
            cell.textLabel.text = distrctModel.name;
//
            if (distrctModel.subdistricts.count) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            } else {
            
                cell.accessoryType = UITableViewCellEditingStyleNone;
            }
        } else {
        
            cell = [ZMDrodownRightTableViewCell dropdownRightTableViewCellWithTableView:tableView];
            
//            ZMDistrictModel *districtModel = self.districtArray[self.selectLeftVCIndex];
//            
//            cell.textLabel.text = districtModel.subdistricts[indexPath.row];
            
            cell.textLabel.text = self.selectDistrictModel.subdistricts[indexPath.row];
        }
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.categoryArray.count) {
        
        if (tableView == self.leftView) {
//            记录选中的左边的分类模型
            self.selectCategoryModel = self.categoryArray[indexPath.row];
            
//            选中左边发送通知 --> 当没有子分类的时候才发送 --> 发送选中的模型
            if (self.selectCategoryModel.subcategories == 0) {
                
                //选中分类的通知
                [ZMNotificationCenter postNotificationName:ZMCategoryDidChangeNotifacation object:nil userInfo:@{ZMSelectCategoryModel : self.selectCategoryModel}];
            }

            
        } else {
         //选中分类的通知
            [ZMNotificationCenter postNotificationName:ZMCategoryDidChangeNotifacation object:nil userInfo:@{ZMSelectCategorySubtitle : self.selectCategoryModel.subcategories[indexPath.row],ZMSelectCategoryModel : self.selectCategoryModel}];
        }
    } else {
    
        if (tableView == self.leftView) {
            
            self.selectDistrictModel = self.districtArray[indexPath.row];
            
            if (self.selectDistrictModel.subdistricts == 0) {
                
//                地区改变的通知
                [ZMNotificationCenter postNotificationName:ZMDistrictDidChangeNotifacation object:nil userInfo:@{ZMSelectDistrictModel : self.selectDistrictModel}];
            }
        
        } else {
            
        //                地区改变的通知
            [ZMNotificationCenter postNotificationName:ZMDistrictDidChangeNotifacation object:nil userInfo:@{ZMSelectDistrictSubtitle : self.selectDistrictModel.subdistricts[indexPath.row],ZMSelectDistrictModel : self.selectDistrictModel}];
        
        }
    }
    
    [self.rightView reloadData];

}

@end
